package com.cdshangpai.utils.storage;

import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.cdshangpai.PhotoappApplication;
import com.cdshangpai.photoapp.LogConfig;

import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class VideoManage {
    private static String videoBaseFolder= FileConfig.baseFolder+File.separator+FileConfig.baseFolder+File.separator+FileConfig.videoFolder;
    /**
     * 下载视频文件
     * @param url
     * @return
     */
    public String download(String url)
    {
        BlockingQueue<String> queue=new LinkedBlockingDeque<>();
        DownloadListenter listenter=new DownloadListenter(queue);
        DownloadUtil.get().download(url,videoBaseFolder,false,listenter);
        String fileName=null;
        try{
            fileName=queue.take();
        }catch (InterruptedException e){
            e.printStackTrace();
            Log.i(LogConfig.logTag,"等待下载返回过程中出现问题");
        }
        return fileName;
    }
    /**
     * 删除视频文件
     * @param fileName
     */
    public void delete(String fileName)
    {
        if(TextUtils.isEmpty(fileName))
        {
            return;
        }
        FileInput fi=new FileInput(PhotoappApplication.context);
        fi.delete(videoBaseFolder,fileName);
        Log.i(LogConfig.logTag,"成功删除视频文件:"+fileName);
    }

    /**
     * android10以上获取文件url
     * @param fileName
     * @return
     */
    public Uri getUri(String fileName)
    {
        return Uri.parse(fileName);
    }



    public File getFile(String fileName)
    {
        String fullFileName=getFullFilename(fileName);
        return new File(fullFileName);
    }

    public String getFullFilename(String fileName)
    {
        return Environment.getExternalStorageDirectory()+File.separator+videoBaseFolder+File.separator+fileName;
    }
}
